package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.RouteTaskRecordDao;
import com.wlhse.dto.inDto.RouteTaskRecordInDto;
import com.wlhse.entity.RouteTaskRecordPojo;
import com.wlhse.service.RouteTaskRecordService;
import com.wlhse.util.UseCountUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class RouteTaskRecordServiceImpl implements RouteTaskRecordService {

    @Resource
    private RouteTaskRecordDao routeTaskRecordDao;

    @Resource
    private UseCountUtil useCountUtil;

    @Value("${RESOURCES_URL}")
    private String url;

    @Override
    public String getRouteTaskRecord(RouteTaskRecordInDto routeTaskRecordInDto, Integer userId) {
        //设置使用频率
        String redisName = "user_" + userId + "_employee_routing_inspection";
        useCountUtil.useCountSetCache(redisName, routeTaskRecordInDto.getRouteEmpID());

        routeTaskRecordInDto.setUrl(url);
        if (StringUtils.isNotBlank(routeTaskRecordInDto.getStartDate()) && StringUtils.isBlank(routeTaskRecordInDto.getEndDate())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//设置日期格式
            routeTaskRecordInDto.setEndDate(df.format(new Date()));// new Date()为获取当前系统时间
        }
        if (routeTaskRecordInDto.getType().equals("all"))
            return NR.r(routeTaskRecordDao.queryRouteTaskRecords(routeTaskRecordInDto));
        int total = routeTaskRecordDao.queryRouteTaskRecordsTotal(routeTaskRecordInDto);
        PageHelper.startPage(routeTaskRecordInDto.getPageIdx(), routeTaskRecordInDto.getPageSize());
        List<RouteTaskRecordPojo> routeTaskRecordPojos = routeTaskRecordDao.queryRouteTaskRecords(routeTaskRecordInDto);
        return NR.r(CodeDict.CODE_MESSAGE_DATA_PAGE_LIST, 0, 0, null, routeTaskRecordPojos, total, routeTaskRecordInDto.getPageIdx());
    }
}
